//
//  HLHomeViewController.swift
//  HLXiMaLaYa
//
//  Created by hulinhu on 2018/1/9.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import Then
import RxSwift
import RxCocoa
import SnapKit
import TYPagerController

// MARK: - 常量
fileprivate struct Metric {
    static let searchBarLeft : CGFloat = 12.0
    static let searchBarRight : CGFloat = 12.0

    static let pagerBarFontSize  = UIFont.systemFont(ofSize: 15.0)
    static let pagerBarHeight : CGFloat = 38.0
}


class HLHomeViewController: HLBaseViewController {
    
   private var myTitleView : UIView?
   private let titles : [String] = ["分类", "推荐", "精品", "直播", "广播"]
   private let  pageVC = TYTabPagerController().then {
        $0.pagerController.scrollView?.backgroundColor = kThemeGainsboroColor
        $0.tabBar.layout.cellWidth = kScreenWidth * 0.2
        $0.tabBar.layout.progressColor = kThemeTomatoColor
        $0.tabBar.layout.selectedTextColor = kThemeTomatoColor
        $0.tabBar.backgroundColor = kThemeGainsboroColor
        $0.tabBar.layout.cellSpacing = 0
        $0.tabBar.layout.cellEdging = 0
        $0.tabBar.layout.normalTextFont = Metric.pagerBarFontSize
        $0.tabBar.layout.selectedTextFont = Metric.pagerBarFontSize
        $0.tabBarHeight = Metric.pagerBarHeight
    }
    
    private var vcs: [UIViewController] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
         initTitleView()
         initPageController()
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        myTitleView?.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(-0.5) // 修正偏差
            make.left.equalToSuperview().offset(Metric.searchBarLeft)
            make.right.equalToSuperview().offset(-Metric.searchBarRight)
        }
    }
}


// MARK:- 初始化协议
extension HLHomeViewController : HLNavTitleable {
    
    static func pagerBarHeight() -> CGFloat {
        
        return Metric.pagerBarHeight
    }
    
    // MARK:- 标题组件
    private func initTitleView() {
        let homeNavBar = HLHomeNavBar()
        homeNavBar.itemClicked = { [weak self] (model) in
            
            guard let `self` = self else { return }
            
            let type = model.type
            switch type {
            case .homeSearchBar:
                self.jump2SearchResult()
                break
                
            case .download, .message, .history:
                
                self.jump2Login()
                break
            default: break
            }
        }
        myTitleView = self.titleView(titleView: homeNavBar)
    }
    
    // MARK:- 分页控制器
    private func initPageController() {
        
        addChildViewController(pageVC)
        view.addSubview(pageVC.view)
        pageVC.delegate = self
        pageVC.dataSource = self
        pageVC.view.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        pageVC.reloadData()
        
        // 设置起始页
        pageVC.pagerController.scrollToController(at: 0, animate: false)
    }
}

// MARK:- 分页控制器协议
extension HLHomeViewController: TYTabPagerControllerDelegate, TYTabPagerControllerDataSource {
    
    func numberOfControllersInTabPagerController() -> Int {
        return titles.count
    }

    func tabPagerController(_ tabPagerController: TYTabPagerController, controllerFor index: Int, prefetching: Bool) -> UIViewController {
        
        if index == 0 {
            let categoryVC = HLCategoryViewController()
            categoryVC.view.backgroundColor = kThemeSnowColor
            return categoryVC
        }
        
        if index == 1 {
            let recommendVC = HLRecommendViewController()
            recommendVC.view.backgroundColor = kThemeSnowColor
            return recommendVC
        }
        
        if index == 2 {
            let boutiqueVC = HLBoutiqueViewController()
            boutiqueVC.view.backgroundColor = kThemeSnowColor
            return boutiqueVC
        }
        
        let vc = UIViewController()
        vc.view.backgroundColor = kThemeWhiteColor
        return vc
    }
    
    func tabPagerController(_ tabPagerController: TYTabPagerController, titleFor index: Int) -> String {
        return titles[index]
    }
}


// MARK:- 控制器跳转
extension HLHomeViewController {
    
    // MARK:- 搜索结果
    func jump2SearchResult() {
        
        let VC = HLNavigationViewController(rootViewController: HLSearchViewController())
        self.present(VC, animated: false, completion: nil)
    }
    
    // MARK:- 下载
    func jump2Login() {
        
        let VC = HLNavigationViewController(rootViewController: HLLoginViewController())
        self.present(VC, animated: true, completion: nil)
    }
}












